$('.select_login>a').click(function(){
    $('.register').css('display','none')
    $('.login').css('display','block')
})
$('.select_register>a').click(function(){
    $('.register').css('display','block')
    $('.login').css('display','none')
})
$('.register_btn').click(function(e){
    e.preventDefault()
    $.ajax({
        type: "post",
        url: "http://localhost:8888/users/register",
        data: {
            username:$('.register #username').val(),
            password:$('.register #password1').val(),
            rpassword:$('.register #password2').val(),
            nickname:$('.register #nickname').val()
        },
        headers:'application/x-www-form-urlencoded',
        dataType: "json",
        success: function (res) {
            if(res.code === 1){
                alert(res.message)
                $('.register').css('display','none')
                $('.login').css('display','block')
            }else{
                alert(res.message)
            }
        },
        error:function(err){
            alert(err.message)
        }
    });
})
$('.login_btn').click(function(e){
    e.preventDefault()
    $.ajax({
        type: "post",
        url: "http://localhost:8888/users/login",
        data: {
            username:$('.login #username').val(),
            password:$('.login #password1').val()
        },
        dataType: "json",
        success: function (res) {
            alert(res.message)
            if(res.code === 1){
                localStorage.setItem('id',res.user.id)
                localStorage.setItem('token',res.token)
                location.href = './index.html'
            }
        },
        error:function(err){
            alert(err.message)
        }
    })
})